package conf

import (
	"fmt"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"os"
)

/*
   Author: mahaiyuan
   Date: 2021/6/1 10:46 下午
   Description: 配置文件处理类
*/
var (
	Resource ResourceConf
)

/*
初始化配置文件
*/
func Init() {
	loadConf("resource.yaml", &Resource)
	fmt.Printf("resource = %#v \n", Resource)
}

func loadConf(filename string, data interface{}) {
	dir, _ := os.Getwd()
	fmt.Println("dir =", dir)
	bytes, err := ioutil.ReadFile(dir + "/conf/" + filename)
	if err != nil {
		panic(fmt.Sprintf("read config file %s error.%v", filename, err))
	}
	fmt.Println("config content is :", string(bytes))
	if err = yaml.Unmarshal(bytes, data); err != nil {
		panic(fmt.Sprintf("unmarshal config file %s error.%v", filename, err))
	}
}

type ResourceConf struct {
	Mysql map[string]MysqlConfig //数据库配置
}

type MysqlConfig struct {
	Username string `yaml:"username"`
	Password string `yaml:"password"`
}
